package com.weme.common.util

import android.Manifest
import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.os.Build
import android.provider.Settings
import android.telephony.TelephonyManager
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.XXPermissions


object DeviceUtils {
    interface DeviceIdCallback {
        fun onDeviceIdReceived(deviceId: String)
        fun onPermissionDenied()
    }
    fun getUniqueDeviceId(context: Context, callback: DeviceIdCallback) {
        XXPermissions.with(context)
            .permission(Manifest.permission.READ_PHONE_STATE)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                    if (allGranted) {
                        val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager?
                        val deviceId = when {
                            telephonyManager != null -> {
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                                    Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
                                } else {
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                        telephonyManager.imei // Requires READ_PHONE_STATE permission
                                    } else {
                                        telephonyManager.deviceId // Requires READ_PHONE_STATE permission
                                    }
                                }
                            }
                            else -> ""
                        }
                        callback.onDeviceIdReceived(deviceId)
                    }
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    callback.onPermissionDenied()
                }
            })
    }

    fun getVersionName(): String? {
        var version: String? = null
        try {
            val packageManager = UtilsApp.getApp().packageManager
            val packInfo = packageManager.getPackageInfo(UtilsApp.getApp().packageName, 0)
            version = packInfo.versionName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return version
    }

    fun getAppVersionCode(): Int {
        try {
            val packageInfo = UtilsApp.getApp().packageManager.getPackageInfo(UtilsApp.getApp().packageName, 0)
            return packageInfo.versionCode
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return -1 // 返回一个默认值，表示获取版本号失败
    }

    fun getDeviceId(): String {

        val deviceId = StringBuilder() // 渠道标志
        deviceId.append("ps")
        try { //序列号（sn）
            val sn = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                Build.getSerial()
            } else {
                Build.SERIAL
            }
            if (sn != null && sn.isNotEmpty()) {
                deviceId.append("sn") //                deviceId.append(sn);
            }
            val androidId = Settings.Secure.getString(UtilsApp.getApp().contentResolver, Settings.Secure.ANDROID_ID)
            if (androidId != null && androidId.isNotEmpty()) {
                deviceId.append("aid")
                deviceId.append(androidId)
            }
        } catch (e: Exception) {
        }
        return deviceId.toString()
    }



    fun getDeviceName(): String? {
        return Build.DEVICE
    }
    fun getDeviceModel(): String? {
        return Build.MODEL
    }
    fun getDeviceBoard(): String? {
        return Build.BOARD
    }
    fun getDeviceManufacturer(): String? {
        return Build.MANUFACTURER
    }

    fun getMac(context: Context): String? {
        if (context == null) {
            return ""
        }
        val wm: WifiManager = context.getApplicationContext().getSystemService(Context.WIFI_SERVICE) as WifiManager
        val wi: WifiInfo = wm.getConnectionInfo()
        return if (wi == null || wi.macAddress == null) {
            ""
        } else wi.macAddress.trim { it <= ' ' }
    }
    fun getOsVersion(): String? {
        return Build.VERSION.RELEASE
    }

    fun getAndroidVersion(): Int {
        return Build.VERSION.SDK_INT
    }

//获取当前apk第一次安装时间
    fun getFirstInstallTime(context:Context): Long {
        if (context == null) {
            return 0
        }
        val pm: PackageManager = context.getPackageManager()
        var packageInfo: PackageInfo? = null
        try {
            packageInfo = pm.getPackageInfo(context.getPackageName(), 0)
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return packageInfo!!.firstInstallTime
    }
    fun getStatusBarHeight(context: Context): Int {
        val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
        return if (resourceId > 0) {
            context.resources.getDimensionPixelSize(resourceId)
        } else {
            0
        }
    }

}
